package com.tengju.user.domain.shared;


import org.apache.commons.lang3.StringUtils;
import org.joda.time.Days;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FORMAT_FILE_NAME = "MM月dd日HH点mm分";
    public static final String DATE_TIME_MINUTE_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DATE_DAY_FORMAT = "yyyy-MM-dd";
    public static final String DATE_MONTH_FORMAT = "yyyy-MM";
    public static final String DATE_CHINESE_FORMAT = "yyyy年MM月dd日";
    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String DATE_FORMAT_MONTH_INT = "yyyyMM";
    public static final String DATE_MONTH_MINUTE_FORMAT = "MM-dd HH:mm";
    public static final String DATE_HOUR_MINUTE_FORMAT = "HH:mm";


    private DateUtil() {
        //
    }

    public static Date parseStringToDate(String format, String time) {
        try {
            if (StringUtils.isBlank(time)) {
                return null;
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.parse(time);
        } catch (ParseException e) {
            throw new DateParseException(format, time);
        }
    }

    public static String parseDateToString(String format, Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date formatDate(String format, Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return parseStringToDate(format, sdf.format(date));
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static String formartLocalDateTime(String formart,LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return localDateTime.format(DateTimeFormatter.ofPattern(formart));
    }



    /**
     * 前/后?天
     *
     * @param d
     * @param day
     * @return
     */
    public static Date rollDay(Date d, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }


    public static Integer formatMonthInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        String monthStr = localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_MONTH_INT));
        return CastUtil.castInt(monthStr);
    }

    public static String formatMonthToStr(Integer month) {
        Date date = parseStringToDate(DATE_FORMAT_MONTH_INT,month + "");
        return parseDateToString(DATE_MONTH_FORMAT, date);
    }

    public static Long toLong(LocalDateTime localDateTime) {
        if (localDateTime == null){
            return 0L;
        }
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return "";
        }
        //跨年
        if (LocalDate.now().getYear() > localDateTime.getYear()) {
            return DateTimeFormatter.ofPattern(DATE_TIME_MINUTE_FORMAT).format(localDateTime);
        }
        //跨天
        if (LocalDate.now().isAfter(localDateTime.toLocalDate())) {
            return DateTimeFormatter.ofPattern(DATE_MONTH_MINUTE_FORMAT).format(localDateTime);
        }

        return DateTimeFormatter.ofPattern(DATE_HOUR_MINUTE_FORMAT).format(localDateTime);
    }


    public static LocalDate dateToLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        return LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

}
